<?php

class PostController extends Controller
{
	public function actionIndex()
	{
			     $page_size =3; 
         $page;
         
         $tag ;
         $post_count = Post::model()->count();
          $count_page=$post_count/$page_size;
          //���� ���� ���, �� ������� ��� �������
          $tags = TagPost::model()->findAll();
	if(!isset($_GET['tag']))
	{
         if(!isset($_GET['page']))
         {         	
         		$post=Post::model()->recently(0,$page_size)->findAll();                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $post=Post::model()->recently( $sdvig,$page_size)->findAll();
           }
        
	}	
	else //���� ��� ���� �� ������� ��� ������� �� ����
	{
		  $tag=$_GET['tag'];
		$post_count = Post::model()->with('TagPost')->count('post_tag_name=:post_tag_name',array(':post_tag_name'=>$tag));
         $count_page=$post_count/$page_size;
	   if(!isset($_GET['page']))
         {         	
         		$post=Post::model()->with('TagPost')->recently(0,$page_size)->findAll('post_tag_name',array(':post_tag_name'=>$tag));
                   
         }
         else
           {
           	$page=$_GET['page'];
             $sdvig = $page_size*($page-1);
             $post=Post::model()->with('TagPost')->recently( $sdvig,$page_size)->findAll('post_tag_name=:post_tag_name',array(':post_tag_name'=>$tag));
           }
	}
    
      
   

  
    $this->render('index', array(       
        'post'=>$post,
        'count_page'=>$count_page+1,
    'tags'=>$tags,
        
    ));
	  
	}

	
	public function actionRead()
	{	
		$tags = TagPost::model()->findAll();
		$id=$_GET['id'];
	 	$post=Post::model()->findByPk($id);
	 	$this->render('read',array('post'=>$post,'tags'=>$tags));
	}
	
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}